package com.mz.ozy.mvp.presenter;

import android.content.Context;

import com.google.gson.Gson;
import com.mz.ozy.base.BasePresenterImpl;
import com.mz.ozy.bean.MeBean;
import com.mz.ozy.bean.UploadBean;
import com.mz.ozy.mvp.model.AccountSetModel;
import com.mz.ozy.mvp.view.AccountSetContract;
import com.mz.ozy.net.MyObserver;
import com.mz.ozy.net.RetrofitUtils;
import com.mz.ozy.utils.RxUtil;
import com.mz.ozy.utils.ToastUtils;

import org.json.JSONObject;

import java.io.File;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;


public class AccountSetPresenter extends BasePresenterImpl<AccountSetContract.View> implements AccountSetContract.Presenter {

    public static final int TYPE_AVATAR = 1;    //头像
    public static final int TYPE_NICKNAME = 2;  //昵称
    public static final int TYPE_ADDRESS = 3;   //所在地区
    public static final int TYPE_INTRODUCE = 4;   //个人简介

    private AccountSetModel mModel;


    public AccountSetPresenter() {
        mModel = new AccountSetModel();

    }

    @Override
    public void onMe(Context context) {
        mModel.onMe(context)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<MeBean>(mView) {
                    @Override
                    public void onSuccesss(MeBean meBean) {
                        mView.onMeSuccess(meBean);
                    }
                });
    }

    @Override
    public void onUpdate(Context context, final int type, String name) {
        mModel.onUpdate(context,type,name)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<JSONObject>(mView) {
                    @Override
                    public void onSuccesss(JSONObject jsonObject) {
                        mView.onUpdateSuccess(type);
                    }
                });
    }

    @Override
    public void onLogout(final Context context) {
        mModel.onLogout(context)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<JSONObject>(mView) {
                    @Override
                    public void onSuccesss(JSONObject jsonObject) {
                        mView.onLogoutSuccess();
                    }
                });
    }

    @Override
    public void onCancelBind(Context context, int type) {
        mModel.onCancelBind(context,type)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<JSONObject>(mView) {
                    @Override
                    public void onSuccesss(JSONObject jsonObject) {
                        mView.onCancelBindSuccess();
                    }
                });
    }

    public void onUpload(Context context, File file){
        RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"),file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
        RetrofitUtils.getApiService(context).upload(body)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new Observer<ResponseBody>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        try {
                            UploadBean bean = new Gson().fromJson(responseBody.string(), UploadBean.class);
                            if(bean.getCode() == 0){
                                onUpdate(context,AccountSetPresenter.TYPE_AVATAR,bean.getData().getSrc());
                            }else{
                                mView.dismissLoading();
                                ToastUtils.showToast("上传失败");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUtils.showToast("上传失败");
                        mView.dismissLoading();
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }


    @Override
    public void onGetAlipayAuth(Context context) {
        mModel.onGetAlipayAuth(context)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<JSONObject>(mView){

                    @Override
                    public void onSuccesss(JSONObject jsonObject) {
                        mView.onGetAlipayAuthSuccess(jsonObject.optString("sdkUrl"));
                    }
                });
    }

    @Override
    public void onAuthBind(Context context, int type, String id) {
        mModel.onAuthBind(context,type,id)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<JSONObject>(mView){

                    @Override
                    public void onSuccesss(JSONObject result) {
                        mView.onAuthBindSuccess();
                    }
                });
    }

}
